package com.qd.admin.controller.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.SpringContextHolder;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.sys.annotation.DataScope;
import com.qd.common.sys.domain.entity.article.ArticleDTO;
import com.qd.common.sys.event.ArticleHintCountEvent;
import com.qd.system.service.article.ArticleFileService;
import com.qd.system.service.article.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 内容发布
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/article")
public class ArticleController {

    private final ArticleService articleService;
    private final ArticleFileService articleFileService;

    /**
     * 新增内容
     */
    @PreAuthorize("@ps.hasPermission('system:notice:save')")
    @PostMapping(value = "/save")
    public ArticleDTO save(@Validated({DefaultGroup.class}) @RequestBody ArticleDTO dto) {
        if (null == dto.getCreateTime()) {
            dto.setCreateTime(LocalDateTime.now());
        }
        return articleService.saveArticle(dto);
    }

    /**
     * 修改内容
     */
    @PreAuthorize("@ps.hasPermission('system:notice:edit')")
    @PutMapping(value = "/update")
    public ArticleDTO update(@Validated({DefaultGroup.class, UpdateGroup.class}) @RequestBody ArticleDTO dto) {
        return articleService.updateArticle(dto);
    }

    /**
     * 删除内容
     */
    @PreAuthorize("@ps.hasPermission('system:notice:delete')")
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        articleService.deleteArticle(ids);
        return ApiResult.success("操作成功");
    }

    /**
     * 内容详情
     */
    @GetMapping(value = "/getById")
    public ArticleDTO getById(@Validated @NotBlank(message = "文章ID不能为空") String articleId, Integer type) {
        final ArticleDTO articleDto = articleService.getByIdDto(articleId);
        if (null == articleDto) {
            throw new ApiException("内容不存在[" + articleId + "]");
        }
        //个人用户查看才统计数量
        if (null != type && type == 1) {
            SpringContextHolder.publishEvent(new ArticleHintCountEvent(articleId));
        }
        return articleDto;
    }

    /**
     * 内容列表
     */
    @GetMapping(value = "/page")
    @DataScope(orgAlias = "art")
    public IPage<ArticleDTO> page(ArticleDTO articleDto) {
        if (null == articleDto) {
            articleDto = new ArticleDTO();
        }
        return articleService.page(PageUtil.buildPage(articleDto), articleDto);
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ps.hasPermission('system:notice:delete')")
    @DeleteMapping(value = "/articleFileDelete")
    public ApiResultWrapper<Object> articleFileDelete(@RequestBody DelDTO<String> delDTO) {
        articleFileService.removeByIds(delDTO.getIds());
        return ApiResult.success("操作成功");
    }
}